package com.clive.controller;

import com.clive.bean.Dishes;
import com.clive.bean.DishesCategory;
import com.clive.service.DishesService;
import com.clive.service.impl.DishesServiceimpl;
import com.clive.vo.DishResult;
import com.clive.vo.DishesVo;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DishController",urlPatterns = {"/servlet-demo/dishController"})
public class DishController extends HttpServlet {
    private DishesService dishesService;
    private ObjectMapper mapper;
    @Override
    //
    public void init() throws ServletException {
        /**
         * 这个是sevrlet生命周期方法
         * 当我们运行tomcat的时候tomcat把这个项目加载到tomcat里面的时候
         * 这个方法就会执行一次
         */
        dishesService = new DishesServiceimpl();
        mapper = new ObjectMapper();
    }
    //这个是sevrlet生命周期方法
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf8");
        /**
         * post请求中文乱码解决
         * 设置编码格式为utf-8
         */
        //添加菜品逻辑
        //在vscode里我们把op定义为了addDishes
        String op = request.getParameter("op");
        if ("addDishes".equals(op)) {
            String dish_name = request.getParameter("dish_name");
            Integer category_id = Integer.valueOf(request.getParameter("category_id"));
            Double price = Double.valueOf(request.getParameter("price"));
            Dishes dishes = new Dishes();
            dishes.setDishName(dish_name);
            DishesCategory category = new DishesCategory();
            category.setId(category_id);
            dishes.setPrice(price);
            dishes.setCategory(category);
            //controller接受页面请求，把数据放到对象里去，调用service
            boolean b = dishesService.addDishes(dishes);
            DishResult result = null;
            String json = null;
            //判断用户输入的数据
            if (b) {
                result = DishResult.ok(null);
                //把数据转为json格式
                json = mapper.writeValueAsString(result);
            }else {
                result = DishResult.error(null);
                json = mapper.writeValueAsString(result);
            }
            //把json格式的数据传回页面
            response.getWriter().print(json);
        }else if ("pageQuery".equals(op)) {
            //展示菜品逻辑
            Integer currentPage = Integer.valueOf(request.getParameter("currentPage"));
            Integer limit = Integer.valueOf(request.getParameter("limit"));
            DishesVo vo = dishesService.findDishesByCurrentPage(currentPage, limit);
            //判断用户输入的数据
            DishResult result = DishResult.ok(vo);
            //把数据转为json格式
            String	json = mapper.writeValueAsString(result);
            //把json格式的数据传回页面
            response.getWriter().print(json);

        }else if ("deleteDishes".equals(op)) {
            //删除菜品逻辑
            Integer dishesId = Integer.valueOf(request.getParameter("deleteId"));
            boolean b = dishesService.deleteDishesById(dishesId);
            DishResult result = null;
            String json = null;
            //判断用户输入的数据
            if (b) {
                result = DishResult.ok(null);
                //把数据转为json格式
                json = mapper.writeValueAsString(result);
            }else {
                result = DishResult.error(null);
                json = mapper.writeValueAsString(result);
            }
            //把json格式的数据传回页面
            response.getWriter().print(json);
        }else if ("findDishById".equals(op)) {
            Integer dishId = Integer.valueOf(request.getParameter("dishId"));
            Dishes dishes = dishesService.findDishesById(dishId);
            String json = mapper.writeValueAsString(DishResult.ok(dishes));
            response.getWriter().print(json);
        }else if ("updateDish".equals(op)) {
            Integer id = Integer.valueOf(request.getParameter("id"));
            String dish_name = request.getParameter("dish_name");
            Integer category_id = Integer.valueOf(request.getParameter("category_id"));
            Double price = Double.valueOf(request.getParameter("price"));
            Dishes dishes = new Dishes();
            dishes.setId(id);
            dishes.setDishName(dish_name);
            dishes.setPrice(price);
            DishesCategory category = new DishesCategory();
            category.setId(category_id);
            dishes.setCategory(category);
            boolean b = dishesService.updateDishes(dishes);
            DishResult result = null;
            String json = null;
            if (b) {
                result = DishResult.ok(null);
                json = mapper.writeValueAsString(result);
            }else {
                result = DishResult.error(null);
                json = mapper.writeValueAsString(result);
            }
            response.getWriter().print(json);
        }
    }

    //这个是sevrlet生命周期方法
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
